﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using TuoiThoProject.DAL;
using Google.GData.Client;
using Google.GData.Extensions;
using Google.GData.Extensions.Location;
using Google.GData.Photos;

namespace TuoiThoProject.BLL
{
    public class AlbumBLL
    {
        NewsDataContext data = new NewsDataContext();

        private PicasaService service;
        private PicasaService Service
        {
            get
            {
                if (service == null)
                {
                    service = new PicasaService("Ibeta youtube");
                }
                return service;
            }
            set
            {
                service = value;
            }
        }

        public List<AlbumEntity> getAlbums_All(string GmailId)
        {
            tblPicasa picasaId = data.tblPicasas.SingleOrDefault(c => c.cGmailId == GmailId);

            List<AlbumEntity> lstAlbum = new List<AlbumEntity>();

            if (picasaId != null)
            {
                this.Service.setUserCredentials(picasaId.cGmailId, picasaId.cPassword);
                AlbumQuery query = new AlbumQuery(PicasaQuery.CreatePicasaUri(picasaId.cGmailId));

                PicasaFeed feed = this.Service.Query(query);

                foreach (PicasaEntry entry in feed.Entries)
                {
                    AlbumAccessor ac = new AlbumAccessor(entry);

                    AlbumEntity album = new AlbumEntity();
                    album.AlbumId = ac.Id;
                    album.Title = entry.Title.Text;
                    album.Summary = entry.Summary.Text;
                    album.NumberPhotos = ac.NumPhotos;
                    album.Access = ac.Access;


                    album.ThumbnailUrl_0 = entry.Media.Thumbnails[0].Attributes["url"] as string;
                    //album.ThumbnailUrl_1 = entry.Media.Thumbnails[1].Attributes["url"] as string;
                    //album.ThumbnailUrl_2 = entry.Media.Thumbnails[2].Attributes["url"] as string;

                    lstAlbum.Add(album);
                }
            }

            return lstAlbum;
        }

        public void AddAlbum(string Title, string Summary, string Access, string GmailId)
        {
           
            AlbumEntry newEntry = new AlbumEntry();
            newEntry.Title.Text = Title;
            newEntry.Summary.Text = Summary;

            AlbumAccessor ac = new AlbumAccessor(newEntry);
            ac.Access = Access;

            tblPicasa picasaId = data.tblPicasas.SingleOrDefault(c => String.Compare(c.cGmailId, GmailId, false) == 0);
            if (picasaId != null)
            {
                this.Service.setUserCredentials(picasaId.cGmailId, picasaId.cPassword);
                Uri feedUri = new Uri(PicasaQuery.CreatePicasaUri(picasaId.cGmailId));
                PicasaEntry createdEntry = (PicasaEntry)this.Service.Insert(feedUri, newEntry);
            }
        }

        public void UpdateAlbum(string AlbumId, string Title, string Summary, string Access, string GmailId)
        {
            tblPicasa picasaId = data.tblPicasas.SingleOrDefault(c => String.Compare(c.cGmailId, GmailId, false) == 0);
            if (picasaId != null)
            {
                this.Service.setUserCredentials(picasaId.cGmailId, picasaId.cPassword);
                AlbumQuery query = new AlbumQuery(PicasaQuery.CreatePicasaUri(picasaId.cGmailId));

                PicasaFeed feed = service.Query(query);

                foreach (PicasaEntry entry in feed.Entries)
                {
                    AlbumAccessor ac = new AlbumAccessor(entry);
                    if (String.Compare(ac.Id, AlbumId, false) == 0)
                    {
                        ac.Access = Access;
                        entry.Title.Text = Title;
                        entry.Summary.Text = Summary;

                        PicasaEntry updatedEntry = (PicasaEntry)entry.Update();
                        break;
                    }
                }
            }
        }

        public void DeleteAlbum(string AlbumId, string GmailId)
        {
            tblPicasa picasaId = data.tblPicasas.SingleOrDefault(c => String.Compare(c.cGmailId, GmailId, false) == 0);
            if (picasaId != null)
            {
                this.Service.setUserCredentials(picasaId.cGmailId, picasaId.cPassword);
                AlbumQuery query = new AlbumQuery(PicasaQuery.CreatePicasaUri(picasaId.cGmailId));

                PicasaFeed feed = service.Query(query);

                foreach (PicasaEntry entry in feed.Entries)
                {
                    AlbumAccessor ac = new AlbumAccessor(entry);
                    if (String.Compare(ac.Id, AlbumId, false) == 0)
                    {
                        entry.Delete();
                    }
                }
            }
        }
    }
}